package cn.wolfcode.mongo.service.impl;

import cn.wolfcode.mongo.domain.User;
import cn.wolfcode.mongo.repository.UserMongoRepository;
import cn.wolfcode.mongo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMongoRepository userMongoRepository;
    @Override
    public void save(User user) {
        userMongoRepository.save(user);
    }

    @Override
    public void delete(String id) {
        userMongoRepository.deleteById(id);
    }

    @Override
    public void update(User user) {
        userMongoRepository.save(user);
    }

    @Override
    public User get(String id) {
        Optional<User> byId = userMongoRepository.findById(id);
        return byId.orElse(null);
    }

    @Override
    public List<User> list() {
        return userMongoRepository.findAll();
    }

    @Override
    public User queryByName(String name) {
        return userMongoRepository.queryByName(name);
    }

}
